package com.repair.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.repair.entity.Announcement;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface AnnouncementMapper extends BaseMapper<Announcement> {
    
    @Insert("INSERT INTO announcement (title, content, type, status, top, created_by) " +
            "VALUES (#{title}, #{content}, #{type}, #{status}, #{top}, #{createdBy})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Announcement announcement);
    
    @Update("UPDATE announcement SET title = #{title}, content = #{content}, " +
            "type = #{type}, status = #{status}, top = #{top} WHERE id = #{id}")
    int update(Announcement announcement);
    
    @Update("UPDATE announcement SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
    
    @Update("UPDATE announcement SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(Long id);
    
    @Select("SELECT * FROM announcement WHERE id = #{id}")
    Announcement findById(Long id);
    
    @Select("SELECT * FROM announcement WHERE status = 1 ORDER BY top DESC, created_at DESC LIMIT #{limit}")
    List<Announcement> findLatest(int limit);
    
    @Select("SELECT * FROM announcement WHERE status = 1 AND (type = 0 OR type = #{userType}) " +
            "ORDER BY top DESC, created_at DESC")
    List<Announcement> findByUserType(Integer userType);
    
    @Select("SELECT * FROM announcement ORDER BY created_at DESC")
    List<Announcement> findAll();
}